﻿<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UCRecommandCategory.ascx.cs"
    Inherits="A_controls_UCRecommandCategory" %>

<script type="text/javascript">
    function RecommandCategory() {
    }

    RecommandCategory.onAdd = function(cid) { };
    RecommandCategory.onCancel = function(cid) { };

    RecommandCategory.UnRecommandCategory = function(arr, oncompleted, onerror) {
        if (!arr || arr.length < 1) {
            if (onerror && typeof (onerror) == 'function')
                onerror.call(this, "CategoryId 列表为空！");
            return;
        }
        var idStr = arr.join(',');
        $.ajax({ url: "ajax.aspx?action=unrecommandcategoryies&categoryids=" + idStr,
            async: false,
            dataType: "json",
            type: 'GET',
            success: function(d) {
                d = eval(d);
                if (d.Result) {
                    if (oncompleted && typeof (oncompleted) == 'function')
                        oncompleted.call(this, d.Result);
                } else if (onerror && typeof (onerror) == 'function') {
                    onerror.call(this, d.Error);
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                if (onerror && typeof (onerror) == 'function')
                    onerror.call(this, errorThrown);
            }
        });
    }

    RecommandCategory.AddRecommandCateogry = function() {
        $('#dialog-recommandCategory').dialog({
            width: 500,
            height: 165,
            modal: true,
            resizable: false,
            title: "推荐目录",
            zIndex: 101,
            close: function() {
                if (RecommandCategory.onCancel && typeof (RecommandCategory.onCancel) == 'function') {
                    RecommandCategory.onCancel();
                }
            }
        });
        if (!RecommandCategory._inited) {
            RecommandCategory._initCategory();
        }
    }

    RecommandCategory._add = function() {
        var cId = $('#recmdcat-sltCategory').cascadeselect('getvalue');
        if (!cId || cId === '@') {
            alert('请选择要推荐的目录。');
            return;
        }
        $.ajax({
            url: "ajax.aspx?action=recommandcategory&cid=" + cId,
            async: false,
            dataType: "json",
            type: 'GET',
            beforeSend: function() {
                $('#dialog-recommandCategory').masklayer('show', { zIndex: 102, Text: '正在保存，请稍候...' });
            },
            success: function(d) {
                d = eval(d);
                if (!d.Error) {
                    if (RecommandCategory.onAdd && typeof (RecommandCategory.onAdd) == 'function') {
                        RecommandCategory.onAdd(cId);
                    }
                    $("#dialog-recommandCategory").dialog("close");
                } else {
                    alert('保存失败！错误信息：' + d.Error);
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert('发送请求失败！错误信息：' + errorThrown);
            },
            complete: function() {
                $('#dialog-recommandCategory').masklayer('hide');
            }
        });
    }

    RecommandCategory._cancel = function() {
        $("#dialog-recommandCategory").dialog("close");
        if (RecommandCategory.onCancel && typeof (RecommandCategory.onCancel) == 'function') {
            RecommandCategory.onCancel();
        }
    }

    RecommandCategory._inited = false;

    RecommandCategory._initCategory = function() {
        var slt = '#recmdcat-sltCategory', container = '#dialog-recommandCategory';

        $(container).masklayer('show', { zIndex: 102, Text: '正在获取目录...' });
        $.ajax({ url: "ajax.aspx?action=getcategories",
            async: false,
            dataType: "json",
            success: function(data) {
                if (data.Result) {
                    data = eval(data.Result);
                    if (data) {
                        RecommandCategory._inited = true;
                        var i = 0, len = data.length, str = '<option value="@">请选择目录</option>';
                        while (i < len) {
                            str += "<option value='" + data[i].CategoryId + "'>" + data[i].CategoryName + "</option>";
                            i++;
                        }
                        $(slt).append(str);
                        $(slt).cascadeselect({
                            url: 'ajax.aspx?action=getcategories',
                            key: 'pid',
                            type: "GET",
                            valuedatafield: 'CategoryId',
                            textdatafield: 'CategoryName',
                            unselectedoption: { value: '@', text: '请选择目录' },
                            beforebinding: function() {
                                $(container).masklayer('show', { zIndex: 102, Text: '正在获取目录...' });
                            },
                            ondatabinding: function(select, data) {
                                if (data.Result) {
                                    return data.Result;
                                }
                                return data;
                            },
                            oncompleted: function() {
                                $(container).masklayer('hide');
                            }
                        });
                    }
                }
            }
        });
        $(container).masklayer('hide');
    }
</script>

<div id="dialog-recommandCategory">
    <p>
        选择目录：<span><select id='recmdcat-sltCategory'></select></span></p>
    <p style="text-align: right;">
        <input type="button" onclick="RecommandCategory._add()" value="添 加" />
        <input type="button" onclick="RecommandCategory._cancel()" value="取 消" /></p>
</div>
